﻿using System;
using System.Collections.Generic;
using GorillaEngine.ICore;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GorillaEngine.Core.Engine
{
    public class SimpleRenderEngine : IRenderEngine{
        public List<IExtendedDrawable> ObjectsToDraw;
        public List<Action<SpriteBatch>> Actions;

        public SimpleRenderEngine(){
            ObjectsToDraw = new List<IExtendedDrawable>();
            Actions = new List<Action<SpriteBatch>>();
        }

        public virtual void Render(SpriteBatch spriteBatch, GameTime gameTime){
            if (Actions.Count == 0 && ObjectsToDraw.Count == 0)
                return;

            spriteBatch.Begin();

            foreach (var extendedDrawable in ObjectsToDraw) {
                extendedDrawable.Draw();
                extendedDrawable.RenderAction.Invoke(spriteBatch);
            }

            foreach (var action in Actions) {
                action.Invoke(spriteBatch);   
            }

            spriteBatch.End();

            ObjectsToDraw.Clear();
            Actions.Clear();
        }

        public virtual void AddObjectsForRendering(List<IExtendedDrawable> drawables) {
            ObjectsToDraw.AddRange(drawables);
        }

        public virtual void AddAdditionalRenderActions(params Action<SpriteBatch>[] actions){
            Actions.AddRange(actions);
        }
    }
}
